/**
 * 
 */
package fr.rcp.file.explorer.controleur;

import java.io.File;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.ide.FileStoreEditorInput;

import fr.rcp.file.explorer.provider.FileFilter;
import fr.rcp.file.explorer.views.Browsers;

/**
 * @author fred
 * 
 */
public class Controleur implements ISelectionChangedListener,
		IDoubleClickListener, ModifyListener {
	private static Controleur selected;
	private TableViewer tableViewer;
	private TreeViewer treeViewer;
	private Browser browser;
	private IWorkbenchWindow window;

	/**
	 * @return the tableViewer
	 */
	public TableViewer getTableViewer() {
		return tableViewer;
	}

	/**
	 * @param tableViewer
	 *            the tableViewer to set
	 */
	public void setTableViewer(TableViewer tableViewer) {
		this.tableViewer = tableViewer;
	}

	/**
	 * @return the treeViewer
	 */
	public TreeViewer getTreeViewer() {
		return treeViewer;
	}

	/**
	 * @param treeViewer
	 *            the treeViewer to set
	 */
	public void setTreeViewer(TreeViewer treeViewer) {
		this.treeViewer = treeViewer;
	}

	/**
	 * @return the browser
	 */
	public Browser getBrowser() {
		return browser;
	}

	/**
	 * @param browser
	 *            the browser to set
	 */
	public void setBrowser(Browser browser) {
		this.browser = browser;
	}

	/**
	 * @return the window
	 */
	public IWorkbenchWindow getWindow() {
		return window;
	}

	/**
	 * @param window
	 *            the window to set
	 */
	public void setWindow(IWorkbenchWindow window) {
		this.window = window;
	}

	/**
	 * @param tableViewer
	 */
	private Controleur() {
		super();
	}

	public static Controleur getIntance() {
		if (selected == null) {
			selected = new Controleur();
			return selected;
		} else
			return selected;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(
	 * org.eclipse.jface.viewers.SelectionChangedEvent)
	 */
	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event
				.getSelection();
		Object selected_file = selection.getFirstElement();
		if (selected_file != null && ((File) selected_file).isDirectory()) {
			String path = ((File) selected_file).getPath();
			tableViewer.setInput(new File(path));
		}
	}

	@Override
	public void doubleClick(DoubleClickEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event
				.getSelection();
		File selected_file = (File) selection.getFirstElement();
		String file_name = selected_file.getName();
		String ext = "extension";
		if (file_name.contains("."))
			ext = file_name.substring(file_name.lastIndexOf("."));
		switch (ext) {
		case ".txt":
			if (selected_file != null) {
				IWorkbenchPage page = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage();
				try {
					IFileStore fileStore = EFS.getStore(selected_file.toURI());
					IEditorInput input = new FileStoreEditorInput(fileStore);
					page.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID);
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
			break;
		case ".exe":

			break;
		case ".html":
			if (selected_file != null) {
				try {
					String value = selected_file.getPath();
					value.replace("\\", "/");
					browser.setUrl("file:///" + value);

					IWorkbenchPage page = PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.findView(Browsers.ID).getSite().getPage();

					page.showView(Browsers.ID);
				} catch (PartInitException e) {
					e.printStackTrace();
				}

				/*
				 * IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
				 * .getBrowserSupport(); IWebBrowser browser; try { browser =
				 * support.createBrowser("Eclipse browser"); browser.openURL(new
				 * URL("file:///C:/eclipse/epl-v10.html")); } catch
				 * (PartInitException e) { e.printStackTrace(); } catch
				 * (MalformedURLException e) { e.printStackTrace(); }
				 */

			}

			break;
		default:
			if (selected_file != null) {
				IWorkbenchPage page = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage();
				try {
					IFileStore fileStore = EFS.getStore(selected_file.toURI());
					IEditorInput input = new FileStoreEditorInput(fileStore);
					page.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID);
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
			break;
		}
	}

	@Override
	public void modifyText(ModifyEvent event) {
		Combo combo = (Combo) event.getSource();
		String value = combo.getText().trim();
		value.replace("/", "\\");
		if (value != null && value.length() > 0) {
			treeViewer.setInput(new File(value));
			treeViewer.addFilter(new FileFilter());
			tableViewer.setInput(new File(value));
			if (combo.indexOf(value) < 0)
				combo.add(value);
		}
	}
}
